package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import es.ats.codington.festival.daos.PlaceDAOImp;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Places;
import es.ats.codington.festival.interfaces.services.IPlaceService;
import es.ats.codington.festival.utils.FERSDataConnection;

public class PlaceDAOService implements IPlaceService{

	int nRows = 0;
	private Connection con;
	PlaceDAOImp placeDAOImp = new PlaceDAOImp();



	public PlaceDAOService() {
		
	}
	
	/**
	 *  GETTER methods for PlaceDAOService class 
	 * @return
	 */
	
	public Places getPlaceByIdService(int placeId){
		Places place;
		try{
			con = FERSDataConnection.getConnection();
			place = placeDAOImp.getPlaceById(placeId, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
			place = new Museum();
			//If there are problems returns an empty museum
		}
		return place;
	}
	/**
	 *  SETTER methods for PlaceDAOService class 
	 * @return
	 */
	public int insertPlaceService(Places places){
		try{
			con = FERSDataConnection.getConnection();
			nRows = placeDAOImp.insertPlace(places, con);
			con.close();
			
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 *  SETTER methods for PlaceDAOService class 
	 * @return
	 */
	public int updatePlaceService(Places places){
		try{
			con = FERSDataConnection.getConnection();
			nRows = placeDAOImp.updatePlace(places, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 *  SETTER methods for PlaceDAOService class 
	 * @return
	 */
	public int deletePlaceService(int placeId){
				
		try{
			con = FERSDataConnection.getConnection();
			nRows = placeDAOImp.deletePlace(placeId, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 *  GETTER methods for PlaceDAOService class 
	 * @return
	 */
	public ArrayList<Places> getAllPlacesService(){
		ArrayList<Places> listPlaces = new ArrayList<Places>();
		try{
			con = FERSDataConnection.getConnection();
			listPlaces = placeDAOImp.getAllPlaces(con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return listPlaces;
	}
	
}
